package au.edu.swin.swinvehicle.corporateclient;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * CorporateClientManagerBean
 * ----------------
 * This is the remote interface implementation of CorporateClient
 *
 *
 * @author Dany Gunawan
 * @lastmodify 2009-05-12
 */
@Stateless
public class CorporateClientManagerBean implements CorporateClientManagerRemote {

    @PersistenceContext
    private EntityManager em;

    public void addCorporateClient(CorporateClientDetailedTO to) throws CorporateClientManagerException {
        try {
            CorporateClientEntity cc = new CorporateClientEntity();
            cc.setData(to);
            em.persist(cc);
             
        } catch (Exception e) {
            throw new CorporateClientManagerException("Cannot add Corporate Client", e);
        }
    }

    public void editCorporateClient(CorporateClientDetailedTO to) throws CorporateClientManagerException {
        try {
            CorporateClientEntity cc = em.find(CorporateClientEntity.class, to.getId());
            cc.setData(to);
            em.persist(cc);
        } catch (Exception e) {
            throw new CorporateClientManagerException("Cannot edit Corporate Client", e);
        }
    }

    public void deleteCorporateClient(Long id) throws CorporateClientManagerException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<CorporateClientTO> getAllCorporateClients() throws CorporateClientManagerException {

        List<CorporateClientTO> results = null;

        try {

            results = new ArrayList<CorporateClientTO>();
            Query allCorporateClientsQuery = em.createNamedQuery("findAllCorporateClients");
            List dbResults = allCorporateClientsQuery.getResultList();

            for (int i = 0; i < dbResults.size(); i++) {
                CorporateClientEntity cc = (CorporateClientEntity) dbResults.get(i);
                results.add(cc.getData());
            }
        } catch (Exception e) {
            throw new CorporateClientManagerException("Cannot get all Corporate Clients", e);
        }

        return results;

    }

    public List<CorporateClientDetailedTO> getAllDetailedCorporateClients() throws CorporateClientManagerException {

        List<CorporateClientDetailedTO> results = null;

        try {

            results = new ArrayList<CorporateClientDetailedTO>();
            Query allCorporateClientsQuery = em.createNamedQuery("findAllCorporateClients");
            List dbResults = allCorporateClientsQuery.getResultList();

            for (int i = 0; i < dbResults.size(); i++) {
                CorporateClientEntity cc = (CorporateClientEntity) dbResults.get(i);
                results.add(cc.getDetailedData());
            }
        } catch (Exception e) {
            throw new CorporateClientManagerException("Cannot get all CorporateClientDetailedTOs", e);
        }

        return results;


    }

    public CorporateClientDetailedTO getCorporateClient(Long id) throws CorporateClientManagerException {

        CorporateClientEntity cc = null;
        CorporateClientDetailedTO to = null;
        try {
            cc = em.find(CorporateClientEntity.class, id);
            to = cc.getDetailedData();
        } catch (Exception e) {
            throw new CorporateClientManagerException("Cannot get Corporate Client by id " + id.longValue(), e);
        }
        return to;

    }
}
